<?php
class Tourismprovidercategory extends CS_Controller
{
    public function _init()
    {
        $this->load->helper(array('common'));
        $this->load->library('pagination');
        $this->load->model('tourism_provider_category_model', 'tourism_provider_category');
        $this->load->model('tourism_goods_base_model','tourism_goods_base');
       
    }

     /**
     * 供应商分类
     */
    public function grid($pg = 1)
    {
        $getData = $this->input->get();
        $getData['uid'] = $this->uid;
        $page_num = 15;
        $num = ($pg - 1) * $page_num;
        $config['first_url'] = base_url('tourismprovidercategory/grid') . $this->pageGetParam($this->input->get());
        $config['suffix'] = $this->pageGetParam($this->input->get());
        $config['base_url'] = base_url('tourismprovidercategory/search');
        $config['total_rows'] = $this->tourism_provider_category->total( $getData );
        $config['uri_segment'] = 3;
        $this->pagination->initialize($config);
        $data['pg_link'] = $this->pagination->create_links();
        $data['page_list'] = $this->tourism_provider_category->page_list($page_num, $num, $getData);
        $data['all_rows'] = $config['total_rows'];
        $data['pg_now'] = $pg;
        $data['pg_num'] = $page_num;
        $this->load->view('tourismprovidercategory/grid', $data);
    }

    /**
     *类别添加
     */
    public function add()
    {
        $this->load->view('tourismprovidercategory/add');
    }

    /**
     *类别添加的处理
    */
    public function addPost()
    {
       $param['category_name'] = trim($this->input->post('category_name',true));
       $param['sort'] = trim($this->input->post('sort',true));
       $param['uid']  = $this->uid;
       $error = $this->validateAdd($param);
       if( !empty($error) ){
       	  $this->error('tourismprovidercategory/grid', '', $error);
       }
       $result = $this->tourism_provider_category->providerCategoryByName($param['category_name'],$param['uid']);
       if( $result->num_rows() > 0  ){
           $this->error('tourismprovidercategory/add', '', '该类别已经添加');
       }
       $status = $this->tourism_provider_category->insertProviderCategory($param);
       if( $status ){
           $this->success('tourismprovidercategory/grid', '', '保存成功');
       } else{
           $this->error('tourismprovidercategory/add', '', '保存失败');
       }
    }

     /**
     *运费模板修改
     */
    public function edit($id)
    {
       $result = $this->tourism_provider_category->findProviderById($id);
       if( $result->num_rows() <= 0 ){
           $this->error('tourismprovidercategory/grid', '', '没发现该Id值');
       }
       $data['category'] = $result->row(0);
       $this->load->view('tourismprovidercategory/edit',$data);
    }
    
     /**
     * 编辑
     * @param unknown $id
     */
    public function editPost($id)
    {
        $param['category_name'] = trim($this->input->post('category_name',true));
        $param['sort'] = trim($this->input->post('sort',true));
        $error = $this->validateAdd($param);
        if( !empty($error) ){
            $this->error('tourismprovidercategory/edit',"$id", $error);
        }
        $status = $this->tourism_provider_category->updateProviderCategory($id,$param);
        if( $status ){
        	$this->success('tourismprovidercategory/grid', '', '更新成功');
        } else {
            $this->error('tourismprovidercategory/edit',"$id", '更新失败');
        }
    }

     /**
     * 删除
     * @param unknown $id
     */
    public function delete($id,$ismain){
    	
        if($ismain == 2){
            $this->error('tourismprovidercategory/grid', '', '对不起，该品类为主推类，若想删除，请先切换主推类');
        }
        $result = $this->tourism_goods_base->findResultByProviderCategoryId($id);
        if($result->num_rows() > 0){
            $this->error('tourismprovidercategory/grid', '', '对不起，该品类你已经使用，若想删除，请先解除商品使用该分类');
        }
        $status = $this->tourism_provider_category->deleteProviderCategory($id);
        if( $status ){
            $this->success('tourismprovidercategory/grid', '', '删除成功！');
        } else {
            $this->error('tourismprovidercategory/grid', '', '删除失败！');
        }
    }
    
    public function setMain($id){
    	
        $uid  = $this->uid;
        $status = $this->tourism_provider_category->updateProviderCategoryMain($id,$uid);
        if( $status ){
            $this->success('tourismprovidercategory/grid', '', '设置成功！');
        } else {
            $this->error('tourismprovidercategory/grid', '', '设置失败！');
        }
    }
    
    /**
    * 检查类别名称是否重负
    */
    public function checkNameRepeat(){
    	
        $category_name = trim( $this->input->get('category_name') );
        $id = trim( $this->input->get('id') );
        $result = $this->tourism_provider_category->providerCategoryByName($category_name,$this->uid);
        if( empty($id) ){  // 添加
           echo ( $result->num_rows() > 0 ) ? "false" : "true" ; exit;
        }else{ //修改
           echo ( $result->num_rows() > 0 && $result->row(0)->id != $id ) ? "false" : "true";exit;
        }
    }
   
     /**
     * 上下架状态
     * @param unknown $id
     * @param unknown $state
     */
    public function upOrDown($id,$state){
    	
       $status = $this->tourism_provider_category->upOrDown($id,$state);
       if( $status ){
           $this->success('tourismprovidercategory/grid', '', '操作成功！');
       }else{
           $this->error('tourismprovidercategory/grid', '', '操作失败！');
       }
    }
    
     /**
     * 验证
     * @param unknown $param
     */
    public function validateAdd($param){
    	
         $error = array();
         if( $this->validateParam($param['category_name']) ){
            $error['category_name'] = '类别不能为空';
         }
         if( $this->validateParam($param['sort']) ){
            $error['sort'] = '排序不能为空';
         }
         if( !is_numeric($param['sort']) ){
            $error['sort_number'] = 'sort值必须为数字';
         }
         return $error;
    }
}